package com.xuecheng.content.jobhandler;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.xuecheng.base.exception.XueChengException;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MessageProcessAbstract;
import com.xuecheng.messagesdk.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author Linzkr
 * @description: TODO 课程发布任务
 * @date 2023/1/21 11:50
 */
@Slf4j
@Component
public class CoursePublishTask extends MessageProcessAbstract {
    @Autowired
    CoursePublishService coursePublishService;
//    课程发布任务执行入口
    //课程发布消息类型
    public static final String MESSAGE_TYPE = "course_publish";
    //任务调度入口
    @XxlJob("CoursePublishJobHandler")
    public void coursePublishJobHandler() throws Exception {
        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        log.debug("shardIndex=" + shardIndex + ",shardTotal=" + shardTotal);
        //参数:分片序号、分片总数、消息类型、一次最多取到的任务数量、一次任务调度执行的超时时间
        process(shardIndex, shardTotal, MESSAGE_TYPE, 5, 60);
    }
//    课程发布业务逻辑
    @Override
    public boolean execute(MqMessage mqMessage) {
        log.debug("开始执行课程发布任务");
//        课程信息静态化
//          拿到课程ID
        Long courseId = Long.parseLong(mqMessage.getBusinessKey1());
//        将静态页面上传到minIO
        generateCourseHtml(mqMessage,courseId);
//        将课程信息存储到索引库
        saveCourseIndex(mqMessage,courseId);
//        存储到Redis

        return true;
    }
//    创建索引
    private void saveCourseIndex(MqMessage mqMessage, Long courseId) {
//      任务ID
        Long mqMessageId= mqMessage.getId();
        int stageTwo = this.getMqMessageService().getStageTwo(mqMessageId);
//        消息幂等性处理
        if (stageTwo>0){
            log.debug("当前阶段是创建课程索引，已经完成不在处理");
            return;
        }
//      调用service创建索引
        coursePublishService.saveCourseIndex(courseId);
        this.getMqMessageService().completedStageTwo(mqMessageId);
    }

    //   生成静态化页面并上传到MinIO
    public void generateCourseHtml(MqMessage mqMessage,long courseId){
        log.debug("开始进行课程静态化，课程ID{}",mqMessage.getBusinessKey1());
//        生成静态文件前的校验
        MqMessageService mqMessageService = this.getMqMessageService();
        Long mqMessageId= mqMessage.getId();
        int stageOne = mqMessageService.getStageOne(mqMessageId);
        if (stageOne>0){
            log.debug("当前阶段是静态化课程信息任务已经完成不在处理");
            return;
        }
//            生成静态的文件
        File file = coursePublishService.generateCourseHtml(courseId);
        if (file==null){
            XueChengException.cast("课程静态化异常");
        }
//            将HTML文件上传到minIO
        coursePublishService.uploadCourseHtml(courseId, file);
//        保存第一阶段状态
        mqMessageService.completedStageOne(mqMessageId);
    }

}
